//
//  YBNormalTreeTableView.m
//  GongXinDangJian
//
//  Created by apple on 17/12/12.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "YBNormalTreeTableView.h"
#import "RATreeView.h"
#import "YBNormalTreeTableViewCell.h"
#define CELLID @"YBNormalTreeTableViewCell"

@interface YBNormalTreeTableView ()<RATreeViewDelegate , RATreeViewDataSource>
@property (nonatomic , strong) RATreeView * treeView;

@property (nonatomic , strong) NSMutableArray * dataArray;

@property (nonatomic , strong) YBNormalTreeModel * currentSelectModel;
@end

@implementation YBNormalTreeTableView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
        self.currentSelectModel = nil;
        [self ybInitMyView];
    }
    return self;
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch * touch = touches.anyObject;
    CGPoint point = [touch locationInView:self];
    if (!CGRectContainsPoint(self.contentView.frame, point)) {
        self.hidden = YES;
    }
}


-(void)ybInitMyView{
    [self addSubview:self.contentView];
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake([UIScreen zj_screenWith] - ItemWidth * 90, 0, ItemWidth * 32, ItemWidth * 22)];
    imageView.image = [UIImage imageNamed:@"normal_treeview_sanjiaoxing"];
    [self.contentView addSubview:imageView];
    [self.contentView addSubview:self.treeView];
}

-(void)reloadViewDataWithArray:(NSMutableArray *)array{
    self.dataArray = [array mutableCopy];
    [self.treeView reloadData];
}


//返回行高
- (CGFloat)treeView:(RATreeView *)treeView heightForRowForItem:(id)item {
    YBNormalTreeModel * currentModel = (YBNormalTreeModel *)item;
    return currentModel.titleHeight;
}
//将要展开
- (void)treeView:(RATreeView *)treeView willExpandRowForItem:(id)item {
    YBNormalTreeModel * currentModel = (YBNormalTreeModel *)item;
    if (currentModel.children.count > 0) {
        currentModel.isOpen = YES;
        YBNormalTreeTableViewCell *cell = (YBNormalTreeTableViewCell *)[treeView cellForItem:item];
        cell.model = currentModel;
    }
    
}
//将要收缩
- (void)treeView:(RATreeView *)treeView willCollapseRowForItem:(id)item {
    YBNormalTreeModel * currentModel = (YBNormalTreeModel *)item;
    if (currentModel.children.count > 0) {
        currentModel.isOpen = NO;
        YBNormalTreeTableViewCell *cell = (YBNormalTreeTableViewCell *)[treeView cellForItem:item];
        cell.model = currentModel;
    }
}
//已经展开
- (void)treeView:(RATreeView *)treeView didExpandRowForItem:(id)item {
}
//已经收缩
- (void)treeView:(RATreeView *)treeView didCollapseRowForItem:(id)item {
}

//# dataSource方法

//返回cell
- (UITableViewCell *)treeView:(RATreeView *)treeView cellForItem:(id)item {
    //获取cell
    YBNormalTreeTableViewCell * cell = [treeView dequeueReusableCellWithIdentifier:CELLID];
    YBNormalTreeModel * currentModel = (YBNormalTreeModel *)item;
    NSInteger level = [treeView levelForCellForItem:item];
    cell.level = level;
    cell.model = currentModel;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
/**
 *  必须实现
 *
 *  @param treeView treeView
 *  @param item    节点对应的item
 *
 *  @return  每一节点对应的个数
 */
- (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item
{
    YBNormalTreeModel *model = item;
    if (item == nil) {
        return self.dataArray.count;
    }
    return model.children.count;
}
/**
 *必须实现的dataSource方法
 *
 *  @param treeView treeView
 *  @param index    子节点的索引
 *  @param item     子节点索引对应的item
 *
 *  @return 返回 节点对应的item
 */
- (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(id)item {
    YBNormalTreeModel *model = item;
    if (item==nil) {
        return self.dataArray[index];
    }
    return model.children[index];
}
//cell的点击方法
- (void)treeView:(RATreeView *)treeView didSelectRowForItem:(id)item {
    //获取当前的层
//    NSInteger level = [treeView levelForCellForItem:item];
    
    YBNormalTreeModel * currentModel = (YBNormalTreeModel *)item;
    if (currentModel.children.count == 0) {
        if (self.currentSelectModel) {
            YBNormalTreeTableViewCell *selectCell = (YBNormalTreeTableViewCell *)[treeView cellForItem:self.currentSelectModel];
            self.currentSelectModel.isSelect = NO;
            selectCell.model = self.currentSelectModel;
        }
        currentModel.isSelect = YES;
        YBNormalTreeTableViewCell *cell = (YBNormalTreeTableViewCell *)[treeView cellForItem:item];
        cell.model = currentModel;
        self.currentSelectModel = currentModel;
        if (self.delegate && [self.delegate respondsToSelector:@selector(treeView:didSelectCellWithModel:)]) {
            [self.delegate treeView:self didSelectCellWithModel:self.currentSelectModel];
        }
    }
    
}
//单元格是否可以编辑 默认是YES
- (BOOL)treeView:(RATreeView *)treeView canEditRowForItem:(id)item {
    return NO;
}


-(RATreeView *)treeView
{
    if (!_treeView) {
        _treeView = [[RATreeView alloc]initWithFrame:CGRectMake(ItemWidth * 30, ItemWidth * 22, [UIScreen zj_screenWith] - ItemWidth * 60, [UIScreen zj_screenHeight] / 2 - ItemWidth * 22) style:RATreeViewStylePlain];
        _treeView.delegate = self;
        _treeView.dataSource = self;
        [_treeView registerClass:[YBNormalTreeTableViewCell class] forCellReuseIdentifier:CELLID];
        _treeView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _treeView.backgroundColor = [UIColor whiteColor];
    }
    return _treeView;
}


-(NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(UIView *)contentView
{
    if (!_contentView) {
        _contentView = [[UIView alloc]initWithFrame:CGRectMake(0, [UIScreen zj_screenHeight] / 2, [UIScreen zj_screenWith], [UIScreen zj_screenHeight] / 2)];
        _contentView.backgroundColor = [UIColor clearColor];
    }
    return _contentView;
}

@end
